package GUI.Ventas;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.exceptions.OutOfStockIngredientsException;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptyOrderException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

public class AgregarVentaMostrador extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Text text;
	OrderVO orden;
	Label error;
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarVentaMostrador(Shell parent, int style, OrderVO o) {
		super(parent, style);
		setText("SWT Dialog");
		orden=o;
		if(orden==null){
			System.out.println("nula");
		}
		
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(557, 320);
		shell.setText(getText());
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("Articulos del pedido");
		label_1.setBounds(10, 35, 138, 14);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(10, 68, 479, 75);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("New Column");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("New Column");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(274);
		tableColumn_2.setText("New Column");
		
		Label precioTotal = new Label(shell, SWT.NONE);
		if (orden!=null && orden.getArts()!=null && orden.getArts().size()>0) {
			BigDecimal aux = new BigDecimal(0);
			for (ArticleVO o : orden.getArts())
				aux = aux.add(new BigDecimal(o.getPrice()));
			precioTotal.setText(aux.toString());
		} else
			precioTotal.setText("0");
		precioTotal.setBounds(158, 188, 74, 14);
		if(orden==null){
			
		}
		else{
			System.out.print("entre");
			double precioT = 0;
			System.out.print("numero de articulos" + orden.getArts().size());
			
			
			for (int i=0;i<orden.getArts().size();i++){
				//System.out.print("hacemela facil");
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(0,orden.getArts().get(i).getName());
				tableItem.setText(1,String.valueOf(orden.getArts().get(i).getPrice()));
				tableItem.setText(2,orden.getEspecif().get(i));
				
				precioT=precioT + orden.getArts().get(i).getPrice();}
				precioTotal.setText(String.valueOf(precioT));
				
				
		}
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setText("PRECIO TOTAL");
		label_2.setBounds(10, 188, 95, 14);
		
	
		
		Label label_4 = new Label(shell, SWT.NONE);
		label_4.setVisible(false);
		label_4.setText("#Mesa");
		label_4.setBounds(347, 5, 39, 14);
		
		text = new Text(shell, SWT.BORDER);
		text.setVisible(false);
		text.setBounds(410, 0, 64, 19);
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			//hell parent, int style, SaleVO sale, int m, int op, OrderVO o
				if(orden==null){
					ArrayList<ArticleVO> arts = new ArrayList<ArticleVO> ();
					orden=new OrderVO(arts);
					ArrayList<String> d = new ArrayList<String>();
					orden.setEspecif(d);
				}
				AgregarArticulosDelivery agregar = new AgregarArticulosDelivery(shell, shell.getStyle(), null, 0, 1, orden );
				shell.setVisible(false);
				try {
					Thread.sleep(5000);
					agregar.open();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button.setText("Agregar Articulo");
		button.setBounds(369, 149, 120, 28);
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SaleProxyMgt aux = ServiceFacade.getInstance().getSaleProxyMgt() ;
				
					
					try {
						try {
							aux.newTakeOutSale(orden);
						} 
						catch (OutOfStockIngredientsException e1) {
							ArrayList<ArticleVO> g =	e1.getArts();
							String m = "No se puede realizar el pedido. Ingredientes faltantes: ";
							for(int i = 0; i<g.size();i++){
							m = m + " " + g.get(i).getName();
							}
							ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), m);
							ventana.open();
							shell.dispose();
							e1.printStackTrace();
						}
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con la Base de Datos");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (EmptyOrderException e1) {
						error.setText("debe de agregarle articulos a la orden");
						
					}
					
					shell.setVisible(false);
			}
			
			
		});
		button_1.setText("Ingresar Orden");
		button_1.setBounds(347, 254, 142, 28);
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(10, 222, 370, 14);
		error.setText("");

	}
}
